package com.wjyxsy;

import com.wjyxsy.bean.UserComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConsumerMain {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        UserComponent userComponent = (UserComponent) context.getBean("userComponent");

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

        Thread thread1 = new Thread(() -> {
            while (true) {
                userComponent.method1("name1");
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            while (true) {
                userComponent.method2("name2");
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread thread3 = new Thread(() -> {
            while (true) {
                userComponent.method3("name3");
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        fixedThreadPool.execute(thread1);
        fixedThreadPool.execute(thread2);
        fixedThreadPool.execute(thread3);

    }


    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.wjyxsy")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
